 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=218 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:06 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Understand details of FORM tags
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Understand details of FORM tags</div>

<div class='main-body'>
 
<br>When building web applications, you should have an excellent understanding of how <tt>&lt;<a href='http://www.w3.org/TR/html4/interact/forms.html'>FORM</a>&gt;</tt> tags work. 

<P>In particular, there are a number of quirky behaviors that should be noted. 
When in doubt, you can verify form behavior in various browsers. 
Since these quirks are often related to browser implementations, and not to the HTML specfication itself, these quirks may change over time.

<h4>SELECT Controls</h4>
Even if the user has made no selection at all in a <tt>&lt;<a href='http://www.w3.org/TR/html4/interact/forms.html#edef-SELECT'>SELECT</a>&gt;</tt> control, <em>browsers will automatically submit the first</em> <tt>&lt;<a href='http://www.w3.org/TR/html4/interact/forms.html#edef-OPTION'>OPTION</a>&gt;</tt> appearing in the body of a <tt>&lt;SELECT&gt;</tt>.
As a workaround, it is often useful to ensure that the first <tt>&lt;OPTION&gt;</tt> is a <em>blank</em> value, instead of a 'real' one.


<h4>File Upload Controls</h4>
Curiously, there is apparently no attribute to specify the <em>text</em> of the button used for <a href='http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2'>file upload</a> controls.
The text is apparently deduced by the browser from language settings on the client, and not by settings in HTML or HTTP headers.

<P>Reminder : if a form includes a file upload control, then it must have :
<ul>
<li><tt>method='POST'</tt>
<li><tt>enctype='multipart/form-data'</tt>
</ul>

<P>Reminder : the Servlet API has poor support for file upload requests.
As soon as a form includes a file upload control, the format of the underlying request is completely changed.
In particular, if no special measures are taken, <em>none</em> of the form's data will be available from <tt>request.getParameter(String)</tt>; this 
applies to <em>all</em> of the form's data, both file upload controls <em>and</em> regular controls.

<P>Tools such as Apache Commons <a href='http://commons.apache.org/fileupload/'>FileUpload</a> are often used to handle such forms.

<h4>Submit Controls</h4>
It is often desirable to distinguish between the appearance of a control, as seen by the end user, and the actual underlying value submitted to the server. 
(This is particularly true in multilingual applications.)
This is indeed possible for all controls, <em>except</em> for the <tt>Submit</tt> control, whose <tt>value</tt> attribute is used <em>both</em> for its visual appearance <em>and</em> for its submitted value.

<P>This is a problem in multilingual applications where forms have more than one submit button (further <a href='TopicActionfbe8-2.html'>discussion</a>).

<h4>Checkbox Controls</h4>
When a <a href='http://www.w3.org/TR/html4/interact/forms.html#checkbox'>checkbox</a> control is checked, its name and value are submitted to the server. 
If the checkbox control is <em>not</em> checked, then it is not submitted at all. 
That is, the server has two possible tasks: if the request parameter is present, then access its value. 
If the request parameter is not submitted at all, then the server must assume some default value such as 'null' or 'false'.

<h4>Disabled Controls and Read-Only Controls</h4>
The <tt><a href='http://www.w3.org/TR/html4/interact/forms.html#adef-readonly'>readonly</a></tt> and <tt><a href='http://www.w3.org/TR/html4/interact/forms.html#adef-disabled'>disabled</a></tt> attributes are distinct, and have different behavior.
The most important distinction is that readonly items are submitted, while disabled items are not.

<P>When rendering reports or listings that include boolean values, it is tempting to use a disabled checkbox.
However, some feel that such disabled checkboxes don't have a very pleasing appearance. 
One alternative is to use an <em>image</em> instead of a checkbox control, to present the boolean value. 
(One might even use an image of a checkbox.)


<h4>Hitting Enter to Submit</h4>
Forms are usually submitted by hitting a <tt>Submit</tt> button.
However, forms can often be submitted just by hitting the <tt>'Enter'</tt> key. 
This behavior is commonly used for web site search boxes.

<P>Be careful that the server can handle <em>both</em> styles of submission - <tt>Enter</tt> key <em>and</em> <tt>Submit</tt> button.


<h4>TEXTAREA Controls</h4>
You often need to exercise care regarding how new lines are handled by your <tt>&lt;<a href='http://www.w3.org/TR/html4/interact/forms.html#h-17.7'>TEXTAREA</a>&gt;</tt> controls.
The non-standard <tt>wrap</tt> attribute can often be added to a <tt>TEXTAREA</tt> to define a desired policy for new line characters.
Its values are :
<PRE>
wrap=(virtual|physical|off)
</PRE>

<h4>Reminder - POST Versus GET</h4>
If an operation has a side-effect (an edit to the database, for example), then <tt><a href='http://www.w3.org/TR/html4/interact/forms.html#adef-method'>method</a>='POST'</tt> is highly recommended.
If an operation has no side-effect (a list or search operation, for example), then <tt>method='GET'</tt> is highly recommended.

<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicActionfbe8-2.html?Id=203'>Beware multilingual submit buttons</a> <br>
 
  
  <a href='TopicAction4762-2.html?Id=221'>Wrap file upload requests</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='218'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=218 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:06 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
